#!/usr/bin/env python
# -*- coding: utf-8 -*-

from ..models import Model

import base64


def get_random_password(login):
    # 加密算法,通过 login 计算密码
    #
    psw = str(base64.b64encode(login.encode("utf-8")), encoding="utf-8")
    return psw

    # s1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    # s2 = 'abcdefghijklmnopqrstuvwxyz'
    # s3 = '0123456789'
    # s4 = '!@$^&*_+-|:<>'
    # ss = s3 + s1 + s3 + s2 + s3 + s4 + s3
    # return ''.join([ss[randint(0, len(ss)-1)] for i in range(randint(10, 16))])


class IotGateway(Model):
    _name = 'iot.gateway'

    # def search(self, *args, **kwargs):
    #     print('in ResPartner')
    #     return super().search(*args, **kwargs)

    # def search_read(self, *args,  **kwargs):
    #     print('in ResPartner',  args, kwargs)
    #     return super().search_read(*args,  **kwargs)

    def get_by_code(self, code):
        res = self.find_or_create([('code', '=', code)], {
            'code': code,
            'name': code,
            'user_id': None
        })
        return res

    def get_user(self, login, password, gateway_id):
        User = self.env['res.users']
        res = User.update_or_create([('login', '=', login)], {
            'login': login,
            'name': login,
            'email': login,
            'password': password,
            'related_gateway_id': gateway_id
        })

        return res

    def register(self, code):
        print('gate way reg')
        gateway_id = self.get_by_code(code)
        login = 'user_for_gateway_' + code
        password = get_random_password(login)
        # print( 'password: ', password)
        user_id = self.get_user(login, password, gateway_id)
        # print( 'user_id: ', user_id)

        return {
            'gateway_id': gateway_id,
            'related_user_id': user_id,
            'code': code,
            'user': login,
            'password': password
        }


class IotDevice(Model):
    _name = 'iot.device'

    def get_by_code(self, code):
        res = self.find_or_create([('code', '=', code)], {
            'code': code,
            'name': code,
            'user_id': None
        })
        return res

    def find_sensor_type(self, code):
        Device = self.env['iot.sensor.type']
        res = Device.search([('code', '=', code)])
        return res and res[0] or None

    def find_sensor(self, device_id, sensor_type_id, code):
        Device = self.env['iot.sensor']

        res = Device.update_or_create([('code', '=', code)], {
            'code': code,
            'name': code,
            'device_id': device_id,
            'sensor_type_id': sensor_type_id
        })
        return res

    def register(self, device_code, sensor_code, sensor_type):
        print(' reg dev,', device_code, sensor_code, sensor_type)
        device_id = self.get_by_code(device_code)
        sensor_type_id = self.find_sensor_type(sensor_type)

        code = '%s-%s' % (device_code, sensor_code)
        sensor_id = self.find_sensor(device_id, sensor_type_id, code)

        return {
            'device_id': device_id,
            'sensor_id': sensor_id,
            'sensor_type_id': sensor_type_id,
            'device_code': device_code,
            'sensor_code': sensor_code,
            'sensor_type': sensor_type,
        }


class IotValue(Model):
    _name = 'iot.value'

    def update_or_create(self,  values):

        print(values)

        return self.rpccall('update_or_create', values)
